package
{
	import org.flixel.*;
	import org.flixel.plugin.photonstorm.*;
	
	/**
	 * ...
	 * @author JAC
	 */
	public class Unit extends Entity
	{
		protected var _speed:Number;
		protected var _speedOffset:Number;
		protected var _animateMovements:Boolean;
		
		public function Unit(i:int = 0, j:int = 0)
		{
			super(i, j);
			
			_speed = 0;
			_speedOffset = 0;
			_animateMovements = true;
			
			addAnimationCallback(animationCallback);
			
			facing = DOWN;
		}
		
		override public function destroy():void
		{
			stopFollowingPath(true);
			
			super.destroy();
		}
		
		override public function update():void
		{
			super.update();
			
			animateMovements();
		}
		
		public function stopMoving():void
		{
			velocity.x = 0;
			velocity.y = 0;
			stopFollowingPath(true);
			pathSpeed = 0;
		}
		
		public function get isMoving():Boolean
		{
			return velocity.x != 0 || velocity.y != 0;
		}
		
		public function get notMoving():Boolean
		{
			return velocity.x == 0 && velocity.y == 0;
		}
		
		public function set speedOffset(newOffset:Number):void
		{
			_speedOffset = newOffset;
		}
		
		public function offsetSpeed(offset:Number = 0, moveForwardAfterOffset:Boolean = true):void
		{
			_speedOffset += offset;
			if (moveForwardAfterOffset)
			{
				moveTowardsFacing();
			}
		}
		
		public function get speed():Number
		{
			return (_speed + _speedOffset < 0 ? 0 : _speed + _speedOffset);
		}
		
		public function setBaseSpeed(newSpeed:Number):void
		{
			_speed = newSpeed;
		}
		
		protected function moveTowardsFacing():void
		{
			stopMoving();
			switch (facing)
			{
				case UP: 
				{
					velocity.y = -speed;
					break;
				}
				case DOWN: 
				{
					velocity.y = speed;
					break;
				}
				case LEFT: 
				{
					velocity.x = -speed;
					break;
				}
				case RIGHT: 
				{
					velocity.x = speed;
					break;
				}
				default: 
				{
					break;
				}
			}
		}
		
		public function stopAtEdge(callback:Function = null):void
		{
			if (outsideOfTheMaze)
			{
				stopMoving();
				translateToTileByPoint(getMidpoint());
				if (callback != null)
				{
					callback();
				}
			}
		}
		
		public function stopAtExit(callback:Function = null):void
		{
			if (atExit)
			{
				stopMoving();
				translateToTileByPoint(getMidpoint());
				if (callback != null)
				{
					callback();
				}
			}
		}
		
		protected function animationCallback(animationName:String, frameNumber:uint, frameIndex:uint):void
		{
			if (animationName == "DYING" && finished)
			{
				onDeath();
			}
		}
		
		protected function onDeath():void
		{
			stopMoving();
			_light.kill();
			super.kill();
		}
		
		private function animateMovements():void
		{
			if (!_animateMovements)
			{
				return;
			}
			
			if (velocity.y < 0)
			{
				play("UP");
				facing = UP;
			}
			else if (velocity.y > 0)
			{
				play("DOWN");
				facing = DOWN;
			}
			else if (velocity.x < 0)
			{
				play("LEFT");
				facing = LEFT;
			}
			else if (velocity.x > 0)
			{
				play("RIGHT");
				facing = RIGHT;
			}
		}
	}
}